package com.foreknow.web;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Constantitem;
import com.foreknow.service.ConstantitemService;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//常数管理
@RestController
@RequestMapping("/constantitem")
public class ConstantitemController {

    @Autowired
    private ConstantitemService constantitemService;

    //根据id查找常数信息 参数 id
    @RequestMapping(value = "/findConstantitemById",method = RequestMethod.POST)
    public String findConstantitemById(@RequestBody Map<String,Object> para) throws JsonProcessingException {

        Integer id = (Integer) para.get("id");
        Constantitem constantitem = constantitemService.selectByPrimaryKey(id);
        HashMap<String,Object> hs=new HashMap<>();
        ObjectMapper objectMapper=new ObjectMapper();
        //判断是否查询成功
        if(constantitem!=null){
            hs.put("meta",new Constance("msg",200));
            hs.put("constantitem",constantitem);
        }else{
            hs.put("meta",new Constance("msg",404));
        }


        return objectMapper.writeValueAsString(hs);
    }



    //根据constanttypeid查找信息 参数 常数类型主键constanttypeid
    @RequestMapping(value = "/findListByConstanttypeid",method = RequestMethod.POST)
    public String findListByConstanttypeid(@RequestBody Map<String,Object> para) throws JsonProcessingException {

        Integer id = (Integer) para.get("id");
        HashMap<String,Object> hs=new HashMap<>();
        ObjectMapper objectMapper=new ObjectMapper();
        List<Constantitem> list = constantitemService.selectByConstantTypeID(id);
        //判断是否查询成功
        if(list.size()>0){
            hs.put("meta",new Constance("msg",200));
            hs.put("list",list);
        }else{
            hs.put("meta",new Constance("msg",404));
        }

        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping("findShow/{id}")
    public List<Constantitem> findShow(@PathVariable("id") Integer ConstantTypeID ) {
        return constantitemService.selectByTypeID(ConstantTypeID);
    }
    @RequestMapping("findById/{id}")
    public Constantitem findById(@PathVariable("id")Integer id){
        return constantitemService.selectByPrimaryKey(id);
    }
    @RequestMapping("deleteById/{id}")
    public void delete(@PathVariable("id") Integer id){
        constantitemService.updateConstantitemByDelmark(id);

    }


    @PostMapping("save")
    public  String save(@RequestBody Constantitem constantitem){
        Integer constanttypeid =constantitem.getConstanttypeid();
        String constantcode=constantitem.getConstantcode();
        String constantname = constantitem.getConstantname();
        Integer delmark = constantitem.getDelmark();

        Constantitem c1= new Constantitem();
        c1.setConstantcode(constantcode);
        c1.setConstanttypeid(constanttypeid);
        c1.setDelmark(delmark);
        c1.setConstantname(constantname);
        int i = constantitemService.save(c1);
        if (i==1){
            return "success";
        }else {
            return "error";
        }
    }

    @PutMapping("update")
    public String update(@RequestBody Constantitem constantitem){
        Integer id=constantitem.getId();
        Integer constanttypeid =constantitem.getConstanttypeid();
        String constantcode=constantitem.getConstantcode();
        String constantname = constantitem.getConstantname();
        Integer delmark = constantitem.getDelmark();

        Constantitem c1= new Constantitem();
        c1.setId(id);
        c1.setConstantcode(constantcode);
        c1.setConstanttypeid(constanttypeid);
        c1.setDelmark(delmark);
        c1.setConstantname(constantname);
        int i = constantitemService.updateByPrimaryKeySelective(c1);
        if (i==1){
            return "success";
        }else {
            return "error";
        }

    }
    @RequestMapping(value = "/listConstantitem")
    @ResponseBody
    public String findByPageByCondition(@RequestBody Map<String,Object> para) throws JsonProcessingException {

        HashMap<String,Object> hs=new HashMap<>();
        ObjectMapper objectMapper=new ObjectMapper();
        Constantitem constantitem =new Constantitem();
        if(para.get("id")!=null){ constantitem.setId((Integer) para.get("id"));}
        if(para.get("constanttypeid")!=null){ constantitem.setConstanttypeid((Integer)para.get("constanttypeid"));}
        if(para.get("constantcode")!=null){ constantitem.setConstantcode((String) para.get("constantcode"));}
        if (para.get("constantname")!=null){constantitem.setConstantname((String)para.get("constantname"));}
        if(para.get("delmark")!=null){constantitem.setDelmark((Integer) para.get("delmark"));}

        Integer pageNum = (Integer)para.get("pageNum");
        Integer pageSize =  (Integer)para.get("pageSize");

        PageHelper.startPage(pageNum,pageSize);
        List<Constantitem> users = constantitemService.selectByTypeID(constantitem.getConstanttypeid());
        PageInfo<Constantitem> pageInfo = new PageInfo<Constantitem>(users);

        hs.put("meta",new Constance("msg",200));
        hs.put("info",pageInfo);

        return objectMapper.writeValueAsString(hs);

    }


}
